package com.gitee.zhanyao.monomer.contonller;

import com.gitee.zhanyao.monomer.entity.SysUser;
import com.gitee.zhanyao.monomer.model.RegisterModel;
import com.gitee.zhanyao.monomer.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@Slf4j
@Tag(name = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @Operation(summary = "用户注册", description = "获取用户帐号基本信息，用于创建用户帐号")
    @PostMapping("/register")
    public ResponseEntity<SysUser> register(@RequestBody RegisterModel registerModel) {
        HttpStatus httpStatus;
        String userId = "";
        SysUser sysUser = new SysUser();
        try {
            userId = sysUserService.register(registerModel);
            sysUser.setId(userId);
            httpStatus = HttpStatus.OK;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return new ResponseEntity<>(sysUser, httpStatus);
    }

    @Operation(summary = "用户获取", description = "根据用户主键获取用户信息")
    @Parameters(
            @Parameter(name = "userId", description = "用户主键", required = true, example = "4028177173b754900173b769f9c40000")
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功"),
            @ApiResponse(responseCode = "500", description = "操作失败")
    })
    @GetMapping("/users/{userId}")
    public ResponseEntity<SysUser> users(@PathVariable("userId") String userId) {
        HttpStatus httpStatus;
        SysUser sysUser = null;
        try {
            sysUser = sysUserService.users(userId);
            httpStatus = HttpStatus.OK;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return new ResponseEntity<>(sysUser, httpStatus);
    }

    @Operation(summary = "用户删除", description = "根据用户主键删除用户信息")
    @Parameters(
            @Parameter(name = "userId", description = "用户主键", required = true, example = "4028177173b754900173b769f9c40000")
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功"),
            @ApiResponse(responseCode = "500", description = "操作失败")
    })
    @DeleteMapping("/deletion/{userId}")
    public ResponseEntity<SysUser> deletion(@PathVariable("userId") String userId) {
        HttpStatus httpStatus;
        SysUser sysUser = new SysUser();
        try {
            sysUserService.deletion(userId);
            sysUser.setId(userId);
            httpStatus = HttpStatus.OK;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return new ResponseEntity<>(sysUser, httpStatus);
    }

    @GetMapping("/current")
    public Authentication getCurrentUser(Authentication authentication) {
        return authentication;
    }

}
